import 'package:blog/app/package_info.dart';
import 'package:blog/model/lightcloud/condition_card.dart';
import 'package:blog/model/project_model.dart';
import 'package:blog/res/r.dart';
import 'package:blog/util/web_util.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutter/material.dart';


class CardWidgetItem extends StatelessWidget {
  final ProjectDetail detail;
  final bool showCategory;
  final bool showDivider;

  const CardWidgetItem(
      {Key? key, this.showCategory = true, this.showDivider = true, required this.detail})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
        child:Column(
      children: <Widget>[
        GestureDetector(
          onTap: () {
            WebUtil.toWebPage(detail);
          },
          child: Padding(
            padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
            child: Stack(
              children: <Widget>[
                ClipRRect(
                    child: Hero(
                        tag: 'tag',
                        child: imgWidget(detail.envelopePic),), //充满容器，可能会被截断
                    borderRadius: BorderRadius.circular(4)),
                Positioned(
                    left: 5,
                    top: 5,
                    child: Opacity(
                        opacity: showCategory ? 1.0 : 0.0, //处理控件显示或隐藏
                        child: ClipOval(
                            child: Container(
                                decoration:
                                    BoxDecoration(color: Colors.grey),
                                height: 30,
                                width: 30,
                                alignment: AlignmentDirectional.center,
                                child: Text(detail.chapterName.substring(0,2),
                                    style: TextStyle(
                                        fontSize: 15, color: Colors.white)))))),
                Positioned(
                    right: 15,
                    bottom: 10,
                    child: ClipRRect(
                        borderRadius: BorderRadius.circular(5),
                        child: Container(
                            decoration: BoxDecoration(color: Colors.black54),
                            padding: EdgeInsets.all(5),
                            child: Text(
                              //分享时间
                              detail.niceDate,
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 12,
                                  fontWeight: FontWeight.bold),
                            ))))
              ],
            ),
          ),
        ),
        _contentTextItem(detail.title),
         _infoTextItem(detail.envelopePic,detail.chapterName,detail.zan),
        /*Container(
          decoration: BoxDecoration(color: Colors.white),
          padding: EdgeInsets.fromLTRB(15, 0, 15, 10),
          child: Row(
            children: <Widget>[
              _authorHeaderImage(detail),
              Expanded(
                  flex: 1,
                  child: Container(
                      padding: EdgeInsets.only(left: 10),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(detail.title,
                              style: TextStyle(
                                  color: Colors.black,
                                  fontSize: 14,
                                  fontWeight: FontWeight.bold),
                              overflow: TextOverflow.ellipsis),
                          Padding(
                              padding: EdgeInsets.only(top: 2),
                              child: Text(
                                  detail.title == null
                                      ? detail.desc
                                      : detail.title,
                                  style: TextStyle(
                                      color: Color(0xff9a9a9a), fontSize: 12)))
                        ],
                      ))),
              IconButton(
                  icon: Icon(Icons.thumb_up_alt_outlined, color: Colors.black38),
                  //TODO: 点赞
                  onPressed: () => print("点赞"))
            ],
          ),
        ),*/
        Offstage(
          offstage: showDivider,
          child: Padding(
              padding: EdgeInsets.only(left: 5, right: 5),
              child: Divider(height: 0.5)),
        )
      ],
      ),
    );
  }
  _contentTextItem(String desc) {
    return Container(
      padding: EdgeInsets.fromLTRB(6, 10, 6, 10),
      child: Text(
        desc,
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(fontSize: 14, color: Colors.black87),
      ),
    );
  }

  _infoTextItem(String url,String nickname,int collectionCount) {
    return Container(
        padding: EdgeInsets.fromLTRB(6, 0, 6, 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Row(
              children: <Widget>[
                PhysicalModel(
                  //类似于ClipRRect
                  color: Colors.transparent,
                  clipBehavior: Clip.antiAlias,
                  borderRadius: BorderRadius.circular(12),
                  child: _authorHeaderImage(url,24,24),
                ),
                Container(
                  padding: EdgeInsets.all(5),
                  width: 80,
                  child: Text(
                    nickname,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(fontSize: 12),
                  ),
                )
              ],
            ),
            Row(
              children: <Widget>[
                Icon(
                  Icons.local_fire_department,
                  size: 14,
                  color: Colors.red,
                ),
                Padding(
                  padding: EdgeInsets.only(left: 3),
                  child: Text(
                    '${collectionCount}',
                    style: TextStyle(fontSize: 12),
                  ),
                )
              ],
            )
          ],
        ));
  }
  Widget _authorHeaderImage(String imgUrl,double width,double height) {
    return InkWell(
      child: ClipOval(
          clipBehavior: Clip.antiAlias,
          child: HeadImgWidget(imgUrl,width,height),
      ),
      //TODO: 跳转用户界面
      onTap: () => WebUtil.toWebPageOther(title: detail.title,link: PackageInfo.webUrl + "/categories/" + detail.chapterId.toString()),
    );
  }

  Widget imgWidget(String imgUrl){
    if(imgUrl == ""){
      return SvgPicture.asset(
        R.assetsImagesDefaultHeader,
          fit: BoxFit.fill,
      );
    }else{
      return Image.network(
          imgUrl,
          fit: BoxFit.fill,
      );
    }
  }

  Widget HeadImgWidget(String imgUrl,double width,double height){
    if(imgUrl == ""){
      return SvgPicture.asset(
        R.assetsImagesDefaultHeader,
        height: height,
        width: width,
        fit: BoxFit.fill,
      );
    }else{
      return Image.network(
        imgUrl,
        height: height,
        width: width,
        fit: BoxFit.fill,
      );
    }
  }
}
